package com.tanhua.dubbo.server.api;


import com.alibaba.dubbo.config.annotation.Service;
import com.mongodb.client.result.DeleteResult;
import com.tanhua.dubbo.server.pojo.UserLike;
import com.tanhua.dubbo.server.vo.PageInfo;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.ArrayList;
import java.util.List;

@Service(version = "1.0.0")
public class UserLikeApiImpl implements UserLikeApi {

    @Autowired
    private MongoTemplate mongoTemplate;


    /**
     * 保存喜欢
     *
     * @param userId
     * @param likeUserId
     * @return
     */
    @Override
    public String saveUserLike(Long userId, Long likeUserId) {
        Query query = Query.query(Criteria.where("userId").is(userId).and("likeUserId").is(likeUserId));
        //判断mongodb中是否存在
        if (mongoTemplate.count(query, UserLike.class) > 0) {
            return null;
        }

        //创建UserLike对象
        UserLike userLike = new UserLike();
        userLike.setId(ObjectId.get());
        userLike.setUserId(userId);
        userLike.setLikeUserId(likeUserId);
        userLike.setCreated(System.currentTimeMillis());

        //添加
        mongoTemplate.save(userLike);

        return userLike.getId().toHexString();
    }

    /**
     * 相互喜欢
     *
     * @param userId
     * @param likeUserId
     * @return
     */
    @Override
    public Boolean isMutualLike(Long userId, Long likeUserId) {
        //好友互相喜欢,即你的喜欢表中有我,我的喜欢表中也有你
        Criteria criteria1 = Criteria.where("userId").is(userId).and("likeUserId").is(likeUserId);
        Criteria criteria2 = Criteria.where("userId").is(likeUserId).and("likeUserId").is(userId);

        Criteria criteria = new Criteria().orOperator(criteria1, criteria2);

        return mongoTemplate.count(Query.query(criteria), UserLike.class) == 2;
    }


    /**
     * 取消喜欢
     *
     * @param userId
     * @param likeUserId
     * @return
     */
    @Override
    public Boolean deleteUserLike(Long userId, Long likeUserId) {
        Query query = Query.query(Criteria.where("userId").is(userId).and("likeUserId").is(likeUserId));
        //删除
        DeleteResult deleteResult = mongoTemplate.remove(query, UserLike.class);
        return deleteResult.getDeletedCount() == 1;
    }


    /**
     * 相互喜欢的数量
     *
     * @param userId
     * @return
     */
    @Override
    public Long queryEachLikeCount(Long userId) {
        //根据用户id查询他喜欢的用户列表
        Query query = Query.query(Criteria.where("userId").is(userId));
        List<UserLike> userLikeList = mongoTemplate.find(query, UserLike.class);

        //获取到所有我喜欢的列表的用户id
        List<Long> likeUserIdList = new ArrayList<>();
        for (UserLike userLike : userLikeList) {
            likeUserIdList.add(userLike.getLikeUserId());
        }

        Query query2 = query.query(Criteria.where("userId").in(likeUserIdList).and("likeUserId").is(userId));

        return mongoTemplate.count(query2, UserLike.class);
    }


    /**
     * 查询喜欢的数据
     *
     * @param userId
     * @return
     */
    @Override
    public Long queryLikeCount(Long userId) {
        return mongoTemplate.count(Query.query(Criteria.where("userId").is(userId)), UserLike.class);
    }


    /**
     * 粉丝的数量
     *
     * @param userId
     * @return
     */
    @Override
    public Long queryFanCount(Long userId) {
        return mongoTemplate.count(Query.query(Criteria.where("likeUserId").is(userId)), UserLike.class);
    }


    /**
     * 查询相互喜欢列表
     *
     * @param userId
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo<UserLike> queryEachLikeList(Long userId, Integer page, Integer pageSize) {
        //我喜欢的列表
        Query query = Query.query(Criteria.where("userId").is(userId));
        List<UserLike> userLikeList = mongoTemplate.find(query, UserLike.class);

        //获取到所有我喜欢的列表的用户id
        List<Long> likeUserIdList = new ArrayList<>();

        for (UserLike userLike : userLikeList) {
            likeUserIdList.add(userLike.getLikeUserId());
        }

        Query query2 = Query.query(Criteria.where("userId").in(likeUserIdList).and("likeUserId").is(userId));
        return queryList(query2, page, pageSize);
    }


    /**
     * 查询我喜欢的列表
     *
     * @param userId
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo<UserLike> queryLikeList(Long userId, Integer page, Integer pageSize) {
        //查询我关注的
        Query query = Query.query(Criteria.where("userId").is(userId));
        return queryList(query, page, pageSize);
    }


    /**
     * 查询粉丝列表
     *
     * @param userId
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo<UserLike> queryFanList(Long userId, Integer page, Integer pageSize) {
        //查询关注我的
        Query query = Query.query(Criteria.where("likeUserId").is(userId));
        return queryList(query, page, pageSize);
    }


    /**
     * 封装集合
     *
     * @param query
     * @param page
     * @param pageSize
     * @return
     */
    private PageInfo<UserLike> queryList(Query query, Integer page, Integer pageSize) {
        PageRequest pageRequest = PageRequest.of(page - 1, pageSize, Sort.by(Sort.Order.desc("created")));
        query.with(pageRequest);

        List<UserLike> userLikeList = mongoTemplate.find(query, UserLike.class);

        PageInfo<UserLike> pageInfo = new PageInfo<>();
        pageInfo.setRecords(userLikeList);
        pageInfo.setTotal(0);
        pageInfo.setPageSize(pageSize);
        pageInfo.setPageNum(page);
        return pageInfo;

    }
}
